<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Lists extends CI_Controller {

	private $pageSize = '20'; //每一页的记录数

	/**
	 * 具体商品列表页面
	 * 
	 * @param int $category_id 当前分类的ID
	 * @param string $type 默认为空,当前列表的排序方式,可以是价格,销售量,上架时间等等
	 * 
	 * @return void
	 */
	public function index($category_id = '',$type='')
	{
		if(!$category_id || !is_numeric($category_id))
		{
			redirect(base_url('lists/index/1'));
		}
		
		$data = array('title'=>'图书列表 - 作业本教材网', 'style' => 'lists/index','category_id'=>$category_id );
		$this->load->model('m_books');
		$this->load->model('m_category');
		$categorys = $this->m_category->selectCategoryById($category_id);
		
		if(!$categorys)
		{
			header("location:".base_url("error"));
			exit;
		}
		
		//如果父级ID为1,则当前的$category_id则为子集的父级ID
		//如果父级ID不为1,则检索出当前ID的父级ID作为$category_id
		$cate_parent = $categorys['0']->cate_parent;
		
		if($cate_parent == 1 || $cate_parent==0 || $cate_parent==1175)
		{
			$parent_id = $category_id;
		}
		else 
		{
			$parent_id = $cate_parent;
		}
		
		//当前分类的父级分类名称
		$sidebar_first = $this->m_category->selectCategoryById($parent_id);
		
		//当前分类的父级分类下所有子集分类
		$sidebar_second = $this->m_category->selectCategory($parent_id);

		//一级分类
		$data['sidebar_first'] = $sidebar_first;
		
		//二级分类
		$data['sidebar_second'] = $sidebar_second;
		
		$category = $this->parseCategory($category_id);
		//下面的代码只能检索出当前分类的子集分类，所以在此处将当前分类放入数组
		$category[] = $category_id;
		
		//where条件直接输入到sql语句中查询
		$sql = "";
		//当该ID下有子分类，则检索出所有子分类，然后找出全部商品
		if($category)
		{
			foreach ($category as $value) 
			{
				$sql .= " OR b.`book_category` = '".$value."'";
			}
		
			$sql = " AND (".substr($sql, 4).")";
		}
		else
		{
			// 当该ID下没有子分类，则就检索出该分类下所有物品
			$sql = "AND b.`book_category`='".$category_id."'";
		}

		//商品的数量
		$currentPage = $this->input->get('p')?$this->input->get('p'):'1';
		$bookAmount = $this->m_books->selectbooksAmount($sql);
		$bookAmount = $bookAmount['0']->bookAmount?$bookAmount['0']->bookAmount:'0';
		$pagination = array('pageSize'=>$this->pageSize,'totalRows'=>$bookAmount,'currentPage'=>$currentPage,'baseUrl'=>base_url().'lists/index/'.$category_id.'/'.$type);
		$this->load->library('pagination',$pagination);

		//面包屑分类，用于检索出导航
		$data['breadparent_ids'] = $this->findAllParent($category_id);
		
		if(count($data['breadparent_ids']) > 1)
		{
			$tmp = $data['breadparent_ids']['0'];
			$data['breadparent_ids']['0'] = $data['breadparent_ids']['1'];
			$data['breadparent_ids']['1'] = $tmp;
		}
		//商品列表
		$data['books']= $this->m_books->selectBooks( ($currentPage - 1) * $this->pageSize , $this->pageSize , $type , $sql);
	
		$this->load->model('m_bookmark');
		
		$this->load->view('header',$data);
		if($category_id >= 1175)
		{
			$this->load->view('lists/stationery');
		}
		else 
		{
			$this->load->view('lists/index');
		}
		
		$this->load->view('footer');
	}
	
	/**
	 * 具体商品详情页面
	 * 
	 * @param int $goods_id 商品的ID
	 * 
	 * @return void
	 */
	public function item($book_id)
	{
		if(!$book_id || !is_numeric($book_id))
		{
			header('location:'.base_url('error'));
			exit;
		}
		$this->load->model('m_bookmark');
		$this->load->model('m_books');
		$this->m_books->reduceBookView($book_id);

		$data = array( 'style' => array('lists/item') );
		
		$this->load->model("m_books");
		$books = $this->m_books->selectBookById($book_id);
		
		$data['books'] = $books;

		if($books)
		{
			$data['title'] = $books['0']->book_name."_作业本教材网_商品详情";
		}
		else
		{
			$data['title'] = "书本不存在_作业本教材网_商品详情";
		}
		

		$this->load->view('header',$data);
		if($books['0']->cate_type == 1 )
		{
			$this->load->view('lists/item');
		}
		else 
		{
			$this->load->view('lists/stationeryItem');
		}
		
		$this->load->view('footer');
	}
	
	public function category($category_id=1,$type=2)
	{
		$data = array("style"=>"lists/category");
		
		$this->load->model('m_category');
		$categorys = $this->m_category->selectCategoryById($category_id);
		
		if(!$categorys)
		{
			header("location:".base_url("error"));
			exit;
		}
		
		//如果父级ID为1,则当前的$category_id则为子集的父级ID
		//如果父级ID不为1,则检索出当前ID的父级ID作为$category_id
		$cate_parent = $categorys['0']->cate_parent;
		
		if($cate_parent == 1 || $cate_parent==0 || $cate_parent==1175)
		{
			$parent_id = $category_id;
			$type="category";
		}
		else 
		{
			$parent_id = $cate_parent;
			$type="index";
		}
		
		//当前分类的父级分类名称
		$sidebar_first = $this->m_category->selectCategoryById($parent_id);
		
		//当前分类的父级分类下所有子集分类
		$sidebar_second = $this->m_category->selectCategory($parent_id);

		//一级分类
		$data['sidebar_first'] = $sidebar_first;
		
		//二级分类
		$data['sidebar_second'] = $sidebar_second;
		
		$data['type']=$type;
		
		$this->load->view('header',$data);
		if($type==2)
		{
			$this->load->view('lists/secCategory');
		}
		else
		{
			$this->load->view('lists/category');
		}
		
		$this->load->view('footer');
	}

	/**
	 * 查找出该父级分类下的所有分类ID
	 * 
	 * @param int   $parent_id 当前分类的父级ID
	 * @param array $result    一个引用的数组,用于存放最终结果,并返回出来
	 * 
	 * @return array $result 该分类下所有分类ID组成的数组
	 */
	public function parseCategory($parent_id=0,&$result = array())
	{
		$this->load->model('m_category');
		$category = $this->m_category->selectCategoryByParentId($parent_id);
		if($category)
		{
			foreach ($category as $value)
			{
				$result[] = $value->cate_id;
				$this->parseCategory($value->cate_id,$result);
			}
		} 
		return $result;
	}

	/**
	 * 查找出当前分类的所有父级分类ID
	 * 
	 * @param int   $category_id 当前分类的ID值
	 * @param array $result      一个引用的数组,用于存放最终结果,并返回出来
	 * 
	 * @return array $result 该分类的所有父级分类组成的数组
	 */
	public function findAllParent($category_id,&$result = array())
	{
		if($category_id == 0)
		{
			return false;
		}

		$this->load->model('m_category');
		$category = $this->m_category->selectCategoryById($category_id);
		
		//var_dump($category);
		
		if($category && $category['0']->cate_parent != '0')
		{
			foreach ($category as $value)
			{
				$result[] = array('id'=>$value->cate_id,'name'=>$value->cate_name);
				$this->findAllParent($value->cate_parent,$result);
			}
		} 
		return $result;
	}
	
	/**
	 * 用户添加收藏
	 */
	public function addCollectAjax()
	{
		if(!$this->session->userdata('user_id'))
		{
			echo '{"error":"10005"}'; //您还没有登录
			exit;
		}
		$book_id = $this->input->get('book_id');
		$user_id = $this->session->userdata('user_id');
		
		$this->load->model('m_bookmark');
		if($this->m_bookmark->selectBookmark($book_id,$user_id))
		{
			$this->m_bookmark->bookmarkLog($book_id,$user_id,'3');
			echo '{"error":"10006"}'; //该商品已经存在于您的收藏夹
			exit;
		}
		if($this->m_bookmark->insertBookmark($book_id,$user_id))
		{
			$this->m_bookmark->bookmarkLog($book_id,$user_id,'1');
			echo '{"success":"10000"}'; //成功
			exit;
		}
	}
	
	/**
	 * 缺货登记
	 */
	public function bookRegistration()
	{
		$this->load->model('m_books');
		$book_id = $this->input->post('book_id');
		$user_phone = $this->input->post('user_phone');
		$book_amount = $this->input->post('book_amount');
		$most_days = $this->input->post('most_days');
		
		if($this->m_books->bookRegistration($book_id , $user_phone , $book_amount , $most_days))
		{
			echo '1';
		}
	}

	

}

/* End of file fenzhong.php */
/* Location: ./application/controllers/fenzhong.php */